package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DiXiaoNie
 * @since 2022/12/18 15:45
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    /**
     * 找到ap用户id
     *
     * @param id id
     * @return {@link ResponseResult }<{@link ApUser }>
     * @author DiXiaoNie
     * @date 2023/01/05
     */
    @Override
    public ResponseResult<ApUser> findApUserById(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = this.getById(id);
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        return ResponseResult.okResult(apUser);
    }
    /**
     * app端登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        //1.判断到底是登录还有游客

        Map<String, Object> map = new HashMap<>();
        //如果是登录操作
        if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
            //2.1 根据手机号去查询数据库表 数据是否存在
            LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUser::getPhone, dto.getPhone());
            ApUser apUser = this.getOne(queryWrapper);

            //2.2如果不存在 直接报错
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "手机号不存在");
            }
            //2.3如果存在 校验密码
            //获取盐
            String salt = apUser.getSalt();
            //获得明文
            String password = dto.getPassword();
            //md5加密处理
            String md5Password = DigestUtils.md5DigestAsHex((password + salt).getBytes());

            //2.4 密码不正确直接报错
            if (!md5Password.equals(apUser.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

            //2.5 如果密码正确 生成token
            String token = AppJwtUtil.getToken(Long.valueOf(apUser.getId()));

            //2.6 封装校验
            map.put("token", token);
            apUser.setSalt("");
            apUser.setPassword("");
            map.put("user", apUser);

        } else {
            //2.游客  同样返回token  id = 0
            map.put("token", AppJwtUtil.getToken(Long.valueOf(0)));
        }
        return ResponseResult.okResult(map);
    }
}
